/**
 * @author Johann Prieur
 */
package mygui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import kernel.*;
import objects.*;
import utils.*;


public class MobileProperties extends Properties {

    private Simulator _kernel;
    private MobileStation _station;
    private JTabbedPane _tabs;
    private GeneralPanel _general;
    private EmissionsPanel _emissions;
    private MovePanel _move;
    private EnergyPanel _energy;
    private ProbesPanel _probes;
    private LifespanPanel _lifespan;
    private DataMobileStation _data;
    
    public MobileProperties(MobileStation station)
    {
	_kernel = Simulator.getSimulator();
	_station = station;
	_data = (DataMobileStation)_station.getProperties();

	_general = new GeneralPanel(_kernel, _data, _station);
	_emissions = new EmissionsPanel(_data);
	_move = new MovePanel(_data);
	_energy = new EnergyPanel(_data);
	_probes = new ProbesPanel(_data);
	_lifespan = new LifespanPanel(_data);

	setTitle("Mobile station properties");

 	_tabs = new JTabbedPane(JTabbedPane.TOP,
 				JTabbedPane.SCROLL_TAB_LAYOUT);
 	
 	_tabs.add(_general, "General");
 	_tabs.add(_emissions, "Emissions");
	_tabs.add(_move, "Trajectory");
	_tabs.add(_energy, "Energy");
 	_tabs.add(_probes, "Probes");
	_tabs.add(_lifespan, "Lifespan");

	// TODO : grab fields from object
	add("North", new ObjectIdCard(Type.wireless,
				      _data.getName(),
				      "Mobile station"));
	add("Center", _tabs);
	pack();
	WindowUtilities.centerFrame(this);
	setVisible(true);
    }
    
    public void apply()
    {
	_general.apply();
	_emissions.apply();
	_move.apply();
	_energy.apply();
	_probes.apply();
	_lifespan.apply();
	_station.setProperties(_data);
	
	if(_general.getMakeModelSelected()){
	    ModelManager.getModelManager().addModel(_general.getModelName(),_data);
	}
	
    }

    private class GeneralPanel extends JPanel implements ActionListener
    {
	private Simulator _kernel;
	private MobileStation _station;
	private DataMobileStation _data;

        private JTextField _name;
	private String _name_tip = "Name of the base station";

	private JSpinner _x;
	private String _x_tip = "x coordinate of the station";

	private JSpinner _y;
	private String _y_tip = "y coordinate of the station";

	private JSpinner _max_reach_length;
	private String _max_reach_length_tip = "Wave length in meters";

	private JSpinner _carrier_rate;
	private String _carrier_rate_tip = "Carrier rate.\n" +
	    "Must be upper or equal to 1.0";

	private JSpinner _max_frequency;
	private String _max_frequency_tip = "Maximum communication frequency";

	private JSpinner _min_frequency;
	private String _min_frequency_tip = "Minimum communication frequency";

	private JSpinner _nb_frequency;
	private String _nb_frequency_tip = "Number of communication frequencies";

	private JCheckBox _make_model;
	private String _make_model_tip = "Convert into model";
	
	private JTextField _model_name;
	private String _model_name_tip = "Model name";
	
	private GeneralPanel(Simulator kernel, DataMobileStation data, 
			     MobileStation station)
	{
	    _kernel = kernel;
	    _data = data;
	    _station = station;
	    setLayout(new BorderLayout());
	    
	    JPanel general = new JPanel();	    
	    general.setLayout(new GridLayout(9,2,4,4));
	    general.setBorder(new EmptyBorder(4,4,4,4));

	    _name = new JTextField(_data.getName());
	    if(_kernel.isWished(_station)){
		_name.setEnabled(false);
	    }

	    _x = new JSpinner(new SpinnerNumberModel(_data.getCoordX(),
						     DataMobileStation.COORD_X_MIN,
						     Simulator.getSimulator().getEnvironment().getSpace().getWidth(),
						     1));
	    _y = new JSpinner(new SpinnerNumberModel(_data.getCoordY(),
						     DataMobileStation.COORD_Y_MIN,
						     Simulator.getSimulator().getEnvironment().getSpace().getHeight(),
						     1));
	    _carrier_rate = new JSpinner(new SpinnerNumberModel());

	    double max = Math.max(Simulator.getSimulator().getEnvironment().getSpace().getWidth(),
			   Simulator.getSimulator().getEnvironment().getSpace().getHeight());
	    _max_reach_length = new JSpinner(new SpinnerNumberModel(_data.getMaxReachRadius(),
 								    DataMobileStation.MAX_REACH_RADIUS_MIN,
 								    max, 1));
 	    _carrier_rate = new JSpinner(new SpinnerNumberModel(_data.getCarrierRate(),
								DataMobileStation.CARRIER_RATE_MIN,
								max, 1));

	    _min_frequency = new JSpinner(new SpinnerNumberModel(_data.getFmin(),
								 DataMobileStation.FMIN_MIN,
								 DataMobileStation.FMIN_MAX,
								 1));
	    _max_frequency = new JSpinner(new SpinnerNumberModel(_data.getFmax(),
								 DataMobileStation.FMAX_MIN,
								 DataMobileStation.FMAX_MAX,
								 1));
	    _nb_frequency = new JSpinner(new SpinnerNumberModel(_data.getNbFreq(),
								 DataMobileStation.FNB_MIN,
								DataMobileStation.FNB_MAX,
								1));
	    _make_model = new JCheckBox(_make_model_tip,false);
	    _model_name = new JTextField("");
	    
	    general.add(new JLabel("Name"));
	    general.add(_name);
	    _name.setToolTipText(_name_tip);

	    general.add(new JLabel("x location"));
	    general.add(_x);
	    _x.setToolTipText(_x_tip);

	    general.add(new JLabel("y location"));
	    general.add(_y);
	    _y.setToolTipText(_y_tip);

	    general.add(new JLabel("Max reach length"));
	    general.add(_max_reach_length);
	    _max_reach_length.setToolTipText(_max_reach_length_tip);

	    general.add(new JLabel("Carrier rate"));
	    general.add(_carrier_rate);		
	    _carrier_rate.setToolTipText(_carrier_rate_tip);

	    general.add(new JLabel("Min communication frequency"));
	    general.add(_min_frequency);
	    _min_frequency.setToolTipText(_min_frequency_tip);

	    general.add(new JLabel("Max communication frequency"));
	    general.add(_max_frequency);
	    _max_frequency.setToolTipText(_max_frequency_tip);

	    general.add(new JLabel("Number of communication frequencies"));
	    general.add(_nb_frequency);
	    _nb_frequency.setToolTipText(_min_frequency_tip);

	    general.add(_make_model);
	    _make_model.setToolTipText(_make_model_tip);
	    _make_model.addActionListener(this);

	    general.add(_model_name);
	    _model_name.setToolTipText(_model_name_tip);
	    _model_name.setEnabled(false);


	    add("North", general);								
	}

	public boolean getMakeModelSelected(){
	    return _make_model.isSelected();
	}

	public String getModelName(){
	    return _model_name.getText();
	}

	public void apply()
	{
	    _data.setName(_name.getText());
	    _data.setCoordX(((Double)_x.getValue()).doubleValue());
	    _data.setCoordY(((Double)_y.getValue()).doubleValue());
	    _data.setMaxReachRadius(((Double)_max_reach_length.getValue()).doubleValue());
	    _data.setCarrierRate(((Double)_carrier_rate.getValue()).doubleValue());
	    _data.setFmin(((Double)_min_frequency.getValue()).floatValue());
	    _data.setFmax(((Double)_max_frequency.getValue()).floatValue());
	    _data.setNbFreq(((Integer)_nb_frequency.getValue()).intValue());
	}

	public void actionPerformed(ActionEvent e)
	{
	    Component source = (Component)e.getSource();
	    if(source == _make_model)
		{
		    _model_name.setEnabled(_make_model.isSelected());
		}
	}

    }

    private class EmissionsPanel extends JPanel implements ActionListener
    {
	private Simulator _kernel;
	private DataMobileStation _data;

	private JTextField _start_time;
	private String _start_time_tip = "Start time in seconds";

	private JComboBox _stations;
	private String _stations_tip = "Available receivers";

	private JButton _add_receiver;
	private String _add_receiver_tip = "Add a receiver";

	private JButton _remove_receiver;
	private String _remove_receiver_tip = "Remove a receiver";

	private DefaultListModel _model;
	private JList _receiver_list;

	private JTextField _datas;
	private String _datas_tip = "Amount of data to send in bytes";

	private JButton _add;
	private String _add_tip = "Add this emission to the list";

	private JButton _remove;
	private String _remove_tip = "Remove the selected emission from the list";

	private DefaultListModel _model_we;
	private JList _emissions;

	private EmissionsPanel(DataMobileStation data)
	{
	    _kernel = Simulator.getSimulator();
	    _data = data;
	    setLayout(new BorderLayout());
	    
	    JPanel emissions = new JPanel();	    
	    emissions.setLayout(new GridLayout(6,2,4,4));
	    emissions.setBorder(new EmptyBorder(4,4,4,4));

 	    _start_time = new JTextField();

	    Vector receivers = new Vector(_kernel.getMobileStation());
	    receivers.addAll(_kernel.getBaseStation());
	    _stations = new JComboBox(receivers);

	    _datas = new JTextField();
	    
	    _add_receiver = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add_receiver.addActionListener(this);
	    _remove_receiver = new JButton(UIM.createImageIcon("images/remove.png",""));
	    _remove_receiver.addActionListener(this);
	    _model = new DefaultListModel();
	    _receiver_list = new JList(_model);
	    _receiver_list.setVisibleRowCount(5);

	    _add = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add.addActionListener(this);
	    _remove = new JButton(UIM.createImageIcon("images/remove.png",""));
	    _remove.addActionListener(this);

	    _model_we = new DefaultListModel();
	    _emissions = new JList(_model_we);
	    if(_kernel.getMode() == Simulator.ADHOC || _kernel.getMode() == Simulator.SENSOR){
		for(Object dwe3: _data.getWishedEmission3())
		    _model_we.addElement(dwe3);

	    }else{
		for(Object dwe: _data.getWishedEmission()){
		    _model_we.addElement(dwe);
		}
	    }

	    emissions.add(new JLabel("Start time"));
	    emissions.add(_start_time);
	    _start_time.setToolTipText(_start_time_tip);

	    emissions.add(new JLabel("Available receivers"));
	    emissions.add(_stations);
	    _stations.setToolTipText(_stations_tip);

	    emissions.add(_add_receiver);
	    _add_receiver.setToolTipText(_add_receiver_tip);
	    
	    emissions.add(_remove_receiver);
	    _remove_receiver.setToolTipText(_remove_receiver_tip);

	    emissions.add(new JLabel("Selected receivers"));
	    JScrollPane scroll = new JScrollPane(_receiver_list);
	    scroll.setPreferredSize(new Dimension(100,30));
	    emissions.add(scroll);

	    emissions.add(new JLabel("Amount of data to send"));
	    emissions.add(_datas);
	    _datas.setToolTipText(_datas_tip);

	    emissions.add(_add);
	    _add.setToolTipText(_add_tip);

	    emissions.add(_remove);
	    _remove.setToolTipText(_remove_tip);

	    add("North", emissions);
	    add("Center", new JScrollPane(_emissions));
	}

	public void actionPerformed(ActionEvent e)
	{
	    Component source = (Component)e.getSource();
	    if(source == _add_receiver)
		{
		    _model.addElement((Station)_stations.getSelectedItem());
		}
	    else if(source == _remove_receiver)
		{
		    int index = _receiver_list.getSelectedIndex();
		    if(index != -1) _model.remove(index);
		}
	    else if(source == _add)
		{
		    int starttime = (Integer.decode(_start_time.getText())).intValue() * 1000;
		    Vector<String> dests = new Vector<String>();
		    for(Enumeration elem=_model.elements();elem.hasMoreElements();)
			dests.add(elem.nextElement().toString());
		    Vector<String> params = new Vector<String>();
		    // Maybe add more parameters, just the data size for now
		    params.add(_datas.getText());
		    if(_kernel.getMode() == Simulator.ADHOC || _kernel.getMode() == Simulator.SENSOR){
			DataWishedEmission3 dws3 = new
			    DataWishedEmission3(starttime, dests, params);
			_model_we.addElement(dws3);
		    }else{
			DataWishedEmission dws = new
			    DataWishedEmission(starttime, dests, params);
			_model_we.addElement(dws);
		    }
		}
	    else if(source == _remove)
		{
		    int index = _emissions.getSelectedIndex();
		    if(index != -1) _model_we.remove(index);		    
		}
	}

	public void apply()
	{
	    if(_kernel.getMode() == Simulator.ADHOC || _kernel.getMode() == Simulator.SENSOR){
		Vector<DataWishedEmission3> we = new Vector<DataWishedEmission3>();
		for(Enumeration e = _model_we.elements() ; e.hasMoreElements() ;) {
		    we.add((DataWishedEmission3)e.nextElement());
		}
		_data.setWishedEmission3(we);
	    }else{
		Vector<DataWishedEmission> we = new Vector<DataWishedEmission>();
		for(Enumeration e = _model_we.elements() ; e.hasMoreElements() ;) {
		    we.add((DataWishedEmission)e.nextElement());
		}
		_data.setWishedEmission(we);
	    }
	}
    }

    private class MovePanel extends JPanel implements ActionListener 
    {
	private Simulator _kernel;
	private DataMobileStation _data;
	
	private JSpinner _x;
	private String _x_tip = "x coordinate of the destination";

	private JSpinner _y;
	private String _y_tip = "y coordinate of the destination";

	private JTextField _speed;
	private String _speed_tip = "Wanted speed in meter per second";

	private JCheckBox _allow_random;

	private JComboBox _types;
	private String _types_tip = "Move type";

	private JComboBox _ai;
	private String _ai_tip = "AI type";

	private JCheckBox _repeat;
	private String _repeat_tip = "Repeat the moves";

	private JButton _add_move;
	private String _add_move_tip = "Add the move";

	private JTextField _duration;
	private String _duration_tip = "Duration of the pause";

	private JButton _add_pause;
	private String _add_pause_tip = "Add the pause";

	private DefaultListModel _model;
	private JList _moves;

	private JButton _remove;
	private String _remove_tip = "Remove the selected item from the move list";
	
	private JTextField _random_duration;
	private String _random_duration_tip = "Duration of the random move";

	private JButton _add_random;
	private String _add_random_tip = "Add the random move";

	private JCheckBox _finish_random;
	private String _finish_random_tip = "Launch an infinite random move at the end";

	private JTextField _random_speed;

	private JCheckBox _allow_random_speed;

	private MovePanel(DataMobileStation data)
	{
	    _kernel = Simulator.getSimulator();
	    _data = data;
	    setLayout(new BorderLayout());

	    JPanel moves = new JPanel();	    
	    moves.setLayout(new GridLayout(6,2,4,4));
	    moves.setBorder(new TitledBorder("Add a move"));
	    
	    JPanel pause = new JPanel();
	    pause.setLayout(new GridLayout(2,2,4,4));
	    pause.setBorder(new TitledBorder("Add a pause"));

	    JPanel random = new JPanel();
	    random.setLayout(new GridLayout(3,2,4,4));
	    random.setBorder(new TitledBorder("Add a random move"));

	    JPanel top = new JPanel(new BorderLayout());
	    top.add("North", random);
	    top.add("Center", moves);
	    top.add("South", pause);

	    _x = new JSpinner(new SpinnerNumberModel(DataTrajectoryMove.XDEST_DEF,
						     DataMobileStation.COORD_X_MIN,
						     _kernel.getEnvironment().getSpace().getWidth(),
						     1));
	    _y = new JSpinner(new SpinnerNumberModel(DataTrajectoryMove.YDEST_DEF,
						     DataMobileStation.COORD_Y_MIN,
						     _kernel.getEnvironment().getSpace().getHeight(),
						     1));
	    _speed = new JTextField(Double.toString(DataTrajectoryMove.SPEED_DEF));  
	    _allow_random = new JCheckBox("Use a random speed");
	    _allow_random.addActionListener(this);
	    String[] types = { "Clockwise 1/4 turn",
	                       "Counterclockwise 1/4 turn",
			       "Clockwise 1/2 turn",
			       "Counterclockwise 1/2 turn",
			       "Straight line" };	    
	    _types = new JComboBox(types);
	    _types.setSelectedItem("Straight line");
	    String[] ais = {"0","1"};//2 not implemented
	    _ai = new JComboBox(ais);

	    _add_move = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add_move.addActionListener(this);

	    _duration = new JTextField(Double.toString(DataTrajectoryMove.SPEED_MIN));
	    _add_pause = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add_pause.addActionListener(this);
	    
	    _model = new DefaultListModel();
	    _moves = new JList(_model);
	    for(Object move: _data.getTrajectory())
		_model.addElement(move);
	    _remove = new JButton(UIM.createImageIcon("images/remove.png",""));
	    _remove.addActionListener(this);

	    _random_duration = new JTextField(Double.toString(DataTrajectoryMove.SPEED_MIN));
	    _add_random = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add_random.addActionListener(this);
	    _random_speed = new JTextField(Double.toString(DataTrajectoryMove.SPEED_DEF));
	    _allow_random_speed = new JCheckBox("Use a random speed");
	    _allow_random_speed.addActionListener(this);

	    _finish_random = new JCheckBox("End with a random move");
	    _repeat = new JCheckBox("Repeat");

	    moves.add(new JLabel("X coordinate"));
	    moves.add(_x);
	    _x.setToolTipText(_x_tip);
	    
	    moves.add(new JLabel("Y coordinate"));
	    moves.add(_y);
	    _y.setToolTipText(_y_tip);
	    
	    moves.add(new JLabel("Trajectory type"));
	    moves.add(_types);
	    _types.setToolTipText(_types_tip);

	    moves.add(new JLabel("Bouncing AI"));
	    moves.add(_ai);
	    _ai.setToolTipText(_ai_tip);

	    moves.add(new JLabel("Maximum speed"));
	    moves.add(_speed);
	    _speed.setToolTipText(_speed_tip);

	    moves.add(_allow_random);
	    moves.add(_add_move);
	    _add_move.setToolTipText(_add_move_tip);

	    pause.add(new JLabel("Duration"));
	    pause.add(_duration);
	    _duration.setToolTipText(_duration_tip);

	    pause.add(new JLabel());
	    pause.add(_add_pause);
	    _add_pause.setToolTipText(_add_pause_tip);

	    random.add(new JLabel("Duration"));
	    random.add(_random_duration);
	    _random_duration.setToolTipText(_random_duration_tip);
	    random.add(new JLabel("Maximum speed"));
	    random.add(_random_speed);
	    random.add(_allow_random_speed);
	    random.add(_add_random);

	    add("North", top);

	    add("Center", new JScrollPane(_moves));

	    JPanel bottom = new JPanel(new GridLayout(3,2,4,4));
	    bottom.add(_remove);
	    bottom.add(_finish_random);
	    _finish_random.setToolTipText(_finish_random_tip);
	    bottom.add(_repeat);
	    _repeat.setToolTipText(_repeat_tip);

	    _remove.setToolTipText(_remove_tip);
	    
	    add("South", bottom);
	}   

	public void actionPerformed(ActionEvent e)
	{
	    Component source = (Component)e.getSource();
	    if(source == _allow_random_speed)
		{
		    //_random_speed.setEnabled(!_allow_random_speed.isSelected());
		}
	    else if(source == _allow_random)
		{
		    //_speed.setEnabled(!_allow_random.isSelected());
		}
	    else if(source == _add_random)
		{
		    DataTrajectoryMove data = 
			new DataTrajectoryMove(Double.parseDouble(_random_duration.getText()),
					       Double.parseDouble(_random_speed.getText()),
					       _allow_random_speed.isSelected());
		    _model.addElement(data);
		}
	    else if(source == _add_move)
		{
		    DataTrajectoryMove data =
			new DataTrajectoryMove(_types.getSelectedIndex(),
					       ((Double)_x.getValue()).doubleValue(),
					       ((Double)_y.getValue()).doubleValue(),
					       Double.parseDouble(_speed.getText()),
					       _allow_random.isSelected());
		    _model.addElement(data);
		}
	    else if(source == _add_pause)
		{
		    DataTrajectoryPause data =
			new DataTrajectoryPause(Double.parseDouble(_duration.getText()));
		    _model.addElement(data);
		}
	    else if(source == _remove)
		{
		    int index = _moves.getSelectedIndex();
		    if(index != -1) _model.remove(index);
		}
	}

	public void apply()
	{
	    Vector t = new Vector();
	    for(Enumeration e = _model.elements() ; e.hasMoreElements() ;) {
		t.add(e.nextElement());
	    }
	    if(_finish_random.isSelected()) t.add(new DataTrajectoryMove());	    
	    // TODO : check on the sorting thing!! (jprieur)
	    _data.setTrajectory(t);
	    _data.setRepeat(_repeat.isSelected());
	    _data.setAiLevel(Integer.parseInt((String)_ai.getSelectedItem()));
	}
    }

    private class EnergyPanel extends JPanel {
	
	private Simulator _kernel;
	private DataMobileStation _data;

	private JSpinner _battery;
	private JSpinner _waiting;
	private JSpinner _speaking;
	private JSpinner _listening;
	private JSpinner _sleeping;
	private JComboBox _strategy;

	private EnergyPanel(DataMobileStation data)
	{
	    _kernel = Simulator.getSimulator();
	    _data = data;
	    setLayout(new BorderLayout());
	    
	    JPanel energy = new JPanel();	    
	    energy.setLayout(new GridLayout(11,1,4,4));
	    energy.setBorder(new EmptyBorder(4,4,4,4));

	    _battery = new JSpinner(new SpinnerNumberModel(_data.getBatteryLevel(),
							   DataMobileStation.BATTERY_LEVEL_MIN,
							   DataMobileStation.BATTERY_LEVEL_MAX,
							   1));
	    double[] consumptions = _data.getConsumption();
	    _waiting = new JSpinner(new SpinnerNumberModel(consumptions[Station.WAITING],
							   DataMobileStation.CONSUMPTION_MIN[Station.WAITING],// *3600000
							   DataMobileStation.CONSUMPTION_MAX[Station.WAITING],
							   1));
	    _speaking = new JSpinner(new SpinnerNumberModel(consumptions[Station.SPEAKING],
							   DataMobileStation.CONSUMPTION_MIN[Station.SPEAKING],// *3600000
							   DataMobileStation.CONSUMPTION_MAX[Station.SPEAKING],
							   1));
	    _listening = new JSpinner(new SpinnerNumberModel(consumptions[Station.LISTENING],
							   DataMobileStation.CONSUMPTION_MIN[Station.LISTENING],// *3600000
							   DataMobileStation.CONSUMPTION_MAX[Station.LISTENING],
							   1));
	    _sleeping = new JSpinner(new SpinnerNumberModel(consumptions[Station.SLEEPING],
							   DataMobileStation.CONSUMPTION_MIN[Station.SLEEPING],// *3600000
							   DataMobileStation.CONSUMPTION_MAX[Station.SLEEPING],
							   1));
	    StrategyChoser sc = new StrategyChoser("comRadiusController");
	    sc.checkSuperclass("ComRadiusController");
	    Vector<Class> strats = sc.getStrategies();
	    Vector<String> strat_labels = new Vector();
	    for(Class strat : strats){
		String[] path = strat.toString().split("[.]");
		strat_labels.add(path[path.length-1]);
	    }
	    _strategy = new JComboBox(strat_labels);
	    Class controller = _data.getController().getClass();
	    String[] path = controller.toString().split("[.]");
	    _strategy.setSelectedItem(path[path.length-1]);

	    energy.add(new JLabel("Initial battery level (%)"));
	    energy.add(_battery);
	    energy.add(new JLabel("Watching consumption (% per hour)"));
	    energy.add(_waiting);
	    energy.add(new JLabel("Listening consumption (% per hour)"));
	    energy.add(_listening);
	    energy.add(new JLabel("Transmitting consumption (% per hour)"));
	    energy.add(_speaking);
	    energy.add(new JLabel("Sleeping consumption (% per hour)"));
	    energy.add(_sleeping);
	    energy.add(new JLabel("Communication radius controller"));
	    energy.add(_strategy);
	    add("North", energy);
	}   

	public void apply()
	{
	    _data.setBatteryLevel(((Double)_battery.getValue()).doubleValue());
	    double[] consumption = new double[Station.NBSTATES-1];
	    consumption[Station.WAITING] = ((Double)_waiting.getValue()).doubleValue();
	    consumption[Station.LISTENING] = ((Double)_listening.getValue()).doubleValue();
	    consumption[Station.SPEAKING] = ((Double)_speaking.getValue()).doubleValue();
	    consumption[Station.SLEEPING] = ((Double)_sleeping.getValue()).doubleValue();
	    _data.setConsumption(consumption);
	    _data.setController((String)_strategy.getSelectedItem());
	}
    }

    private class LifespanPanel extends JPanel implements ActionListener 
    {
	private Simulator _kernel;
	private DataMobileStation _data;

	private JTextField _birth;
	private JTextField _death;
	
	private ButtonGroup _group;
	private JRadioButton _active;
	private JRadioButton _inactive;
	private JTextField _change;

	private JButton _add;
	private JButton _remove;

	private DefaultListModel _model;
	private JList _list;

	private LifespanPanel(DataMobileStation data)
	{
	    _kernel = Simulator.getSimulator();
	    _data = data;
	    setLayout(new BorderLayout());
	    
	    JPanel top = new JPanel(new GridLayout(2,2,4,4));
	    top.setBorder(new TitledBorder("Lifespan"));
	    JPanel bot = new JPanel(new BorderLayout());
	    bot.setBorder(new TitledBorder("Activity"));
	    JPanel activity = new JPanel(new GridLayout(3,2,4,4));

	    _birth = new JTextField(Integer.toString(_data.getBirth()));
	    _death = new JTextField(Integer.toString(_data.getDeath()));
	    
	    _group = new ButtonGroup();
	    _active = new JRadioButton("active");
	    _inactive = new JRadioButton("inactive");
	    _group.add(_active);
	    _group.add(_inactive);
	    _change = new JTextField();

	    _add = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add.addActionListener(this);
	    _remove = new JButton(UIM.createImageIcon("images/remove.png","")); 
	    _remove.addActionListener(this);

	    _model = new DefaultListModel();
	    for(Object a : _data.getActivities()){
		_model.addElement(a);
	    }
	    _list = new JList(_model);

	    top.add(new JLabel("Birth date"));
	    top.add(_birth);
	    top.add(new JLabel("Death date"));
	    top.add(_death);

	    activity.add(_active);
	    activity.add(_inactive);
	    activity.add(new JLabel("Change date"));
	    activity.add(_change);
	    activity.add(_add);
	    activity.add(_remove);
	    
	    bot.add("North", activity);
	    bot.add("Center", new JScrollPane(_list));
	    add("North", top);
	    add("Center", bot);
	}   

	public void actionPerformed(ActionEvent e)
	{
	    Component source = (Component)e.getSource();
	    if(source == _add)
		{
		    _model.addElement(new Integer(_change.getText()));
		}
	    else if(source == _remove)
		{
		    int index = _list.getSelectedIndex();
		    if(index != -1) _model.remove(index);
		}
	}

	public void apply()
	{
	    _data.setBirth(Integer.parseInt(_birth.getText()));
	    _data.setDeath(Integer.parseInt(_death.getText()));
	    Stack activities = new Stack();
	    for(Enumeration e = _model.elements() ; e.hasMoreElements() ;) {
		activities.push(e.nextElement());
	    }
	    _data.setActivities(activities);
	    // Activities are shit. For now, it's a stack of Integer objects
	    // we should create DataActivity and deal with it.
	}
    }

    private class ProbesPanel extends JPanel implements ActionListener {
	
	private Simulator _kernel;
	private DataMobileStation _data;

	private JComboBox _probes;

	private JButton _add;
	private JButton _remove;
	private DefaultListModel _model;
	private JList _list;

	private ProbesPanel(DataMobileStation data)
	{
	    _kernel = Simulator.getSimulator();
	    _data = data;
	    setLayout(new BorderLayout());
	    
	    JPanel top = new JPanel(new BorderLayout());
	    JPanel toptop = new JPanel(new GridLayout(1,2,0,0));
	    JPanel topbot = new JPanel(new GridLayout(1,2,0,0));
	    top.add("North", toptop);
	    top.add("South", topbot);
	   	    
	    Vector<String> strat_labels = new Vector();
	    
	    StrategyChoser scCom = new StrategyChoser("results");	    
	    scCom.checkSuperclass("ProbeCom");
	    Vector<Class> stratsCom = scCom.getStrategies();

	    StrategyChoser scMove = new StrategyChoser("results");
	    scMove.checkSuperclass("ProbeMove");
	    Vector<Class> stratsMove = scMove.getStrategies();

	    StrategyChoser scEnergy = new StrategyChoser("results");
	    scEnergy.checkSuperclass("ProbeEnergy");
	    Vector<Class> stratsEnergy = scEnergy.getStrategies();

	    for(Class strat : stratsCom){
		String[] path = strat.toString().split("[.]");
		strat_labels.add(path[path.length-1]);
	    }
	    for(Class strat : stratsMove){
		String[] path = strat.toString().split("[.]");
		strat_labels.add(path[path.length-1]);
	    }
	    for(Class strat : stratsEnergy){
		String[] path = strat.toString().split("[.]");
		strat_labels.add(path[path.length-1]);
	    }
	    
	    _probes = new JComboBox(strat_labels);
	    
	    _add = new JButton(UIM.createImageIcon("images/add.png",""));
	    _add.addActionListener(this);
	    _remove = new JButton(UIM.createImageIcon("images/remove.png",""));
	    _remove.addActionListener(this);
	    _model = new DefaultListModel();
	    _list = new JList(_model);
	    for(Object probe: _data.getProbes())
		_model.addElement(probe);

	    toptop.add(new JLabel("Probe types"));
	    toptop.add(_probes);

	    topbot.add(_add);
	    topbot.add(_remove);
	    
	    add("North", top);
	    add("Center", new JScrollPane(_list));
	}   

	public void actionPerformed(ActionEvent e)
	{
	    Component source = (Component)e.getSource();
	    if(source == _add)
		{
		    _model.addElement((String)_probes.getSelectedItem());
		}
	    else if(source == _remove)
		{
		    int index = _list.getSelectedIndex();
		    if(index != -1) _model.remove(index);
		}
	}

	public void apply()
	{
	    Vector probes = new Vector();
	    for(Enumeration e = _model.elements() ; e.hasMoreElements() ;) {
		probes.add(e.nextElement());
	    }
	    _data.setProbes(probes);
	}
    }
}
